import { resolve } from 'path';
import { defineConfig, externalizeDepsPlugin, loadEnv, UserConfig } from 'electron-vite';
import AutoImport from 'unplugin-auto-import/vite';
import Components from 'unplugin-vue-components/vite';
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers';

import vue from '@vitejs/plugin-vue';

export default defineConfig(({ mode }): UserConfig => {
  const env = loadEnv(mode, process.cwd());
  return {
    main: {
      plugins: [externalizeDepsPlugin()]
    },
    preload: {
      plugins: [externalizeDepsPlugin()]
    },
    renderer: {
      resolve: {
        alias: {
          '@renderer': resolve('src/renderer/src')
        }
      },
      server: {
        // 允许IP访问
        host: '0.0.0.0',
        // 应用端口 (默认:3000)
        port: Number(env.VITE_APP_PORT),
        // 运行是否自动打开浏览器
        open: false,
        proxy: {
          /**
           * env.VITE_APP_BASE_API: /dev-api
           */
          [env.VITE_APP_BASE_API]: {
            changeOrigin: true,
            // 线上接口地址
            target: 'http://192.168.31.163:8558',
            // 开发接口地址
            // target: "http://192.168.31.163:8551",
      
            rewrite: (path) => path.replace('/api', '')
          }
        }
      },
      plugins: [
        vue(),
        AutoImport({
          resolvers: [ElementPlusResolver()]
        }),
        Components({
          resolvers: [ElementPlusResolver()]
        })
      ]
    }
  };
});
